package com.xbongbong.paas.service.impl;

import com.xbongbong.i18n.util.I18nMessageUtil;
import com.xbongbong.paas.constant.PaasConstant;
import com.xbongbong.paas.domain.entity.ext.PaasFormDataEntityExt;
import com.xbongbong.paas.domain.entity.ext.PaasFormEntityExt;
import com.xbongbong.paas.enums.SheetTypeEnum;
import com.xbongbong.paas.field.FieldAttrEntity;
import com.xbongbong.paas.service.BusinessButtonService;
import com.xbongbong.paas.toolbox.exception.XbbException;
import com.xbongbong.paas.toolbox.wrap.UserVO;
import com.xbongbong.parent.help.CommonPermissionHelp;
import com.xbongbong.pro.enums.DistributorMarkEnum;
import com.xbongbong.pro.enums.permission.enums.DeleteProPermissionAliasEnum;
import com.xbongbong.pro.enums.permission.enums.PrintProPermissionAliasEnum;
import com.xbongbong.pro.enums.permission.enums.UpdateProPermissionAliasEnum;
import com.xbongbong.pro.form.pojo.TopPermissionsPoJo;
import com.xbongbong.pro.formdata.pojo.dto.FormDataDetailDTO;
import com.xbongbong.pro.permission.pojo.dto.VerifyViewPermissionDTO;
import com.xbongbong.saas.enums.OperateTypeEnum;
import com.xbongbong.saas.enums.XbbRefTypeEnum;
import com.xbongbong.saas.service.toolbox.help.UserTeamHelp;
import com.xbongbong.sys.help.ProPermissionHelp;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.Map;
import java.util.Objects;
import java.util.Set;

/**
 * @author 吴峰
 * @date 2022/05/18 21:21
 */
@Service("businessButtonServiceImpl")
public class BusinessButtonServiceImpl implements BusinessButtonService {
    @Resource
    private UserTeamHelp userTeamHelp;
    @Resource
    private ProPermissionHelp proPermissionHelp;
    @Resource
    private CommonPermissionHelp commonPermissionHelp;

    @Override
    public List<TopPermissionsPoJo> dingtalkDetailCommon(UserVO loginUser, PaasFormEntityExt paasFormEntityExt, Map<String, FieldAttrEntity> explainMap,  PaasFormDataEntityExt paasFormDataEntityExt) throws XbbException {
        Integer businessType = paasFormEntityExt.getBusinessType();
        Integer distributorMark = paasFormEntityExt.getDistributorMark();
        Integer saasMark = paasFormEntityExt.getSaasMark();
        Long menuId = paasFormEntityExt.getMenuId();
        List<Integer> noEditButtonBusinessType = Arrays.asList(XbbRefTypeEnum.COMMUNICATE_PLAN.getCode(), XbbRefTypeEnum.EXPENSE.getCode(), XbbRefTypeEnum.CUSTOMER_STATEMENT_STATISTIC.getCode());
        Boolean isExpenseManagement = Objects.equals(businessType, XbbRefTypeEnum.REIMBURSEMENT_FORM.getCode()) || Objects.equals(businessType, XbbRefTypeEnum.EXPENSE.getCode());
        Set<String> loginUserPermSet = loginUser.getPermSet();
        SheetTypeEnum sheetTypeEnum = SheetTypeEnum.getByBusinessType(businessType);
        boolean sheetFlag = SheetTypeEnum.validateSheet(sheetTypeEnum);
        FormDataDetailDTO formDataDetailDTO = new FormDataDetailDTO(paasFormDataEntityExt.getId(), saasMark, businessType, menuId, paasFormDataEntityExt.getCreatorId(), loginUser);
        //获取编辑权限
        UpdateProPermissionAliasEnum updateProPermissionAliasEnum = UpdateProPermissionAliasEnum.getByCode(businessType);
        boolean hasFormEditPermission = loginUser.getPermSet().contains(updateProPermissionAliasEnum.getAlias());
        VerifyViewPermissionDTO verifyViewPermissionDTO = new VerifyViewPermissionDTO(loginUser, paasFormDataEntityExt);
        boolean hasDataEditpermission = commonPermissionHelp.checkEditPermissionWithoutShareEdit(formDataDetailDTO, verifyViewPermissionDTO, explainMap);
        //获取删除权限
        DeleteProPermissionAliasEnum deleteProPermissionAliasEnum = DeleteProPermissionAliasEnum.getByCode(businessType);
        boolean hasFormDeletePermission = loginUserPermSet.contains(deleteProPermissionAliasEnum.getAlias());

        List<TopPermissionsPoJo> topPermissions = new ArrayList<>();
        // 信用管理相关表单无编辑和删除按钮
        boolean creditFlag = Objects.equals(businessType, XbbRefTypeEnum.CREDIT_LIMIT.getCode()) || Objects.equals(businessType, XbbRefTypeEnum.CREDIT_TEMPORARY_LIMIT.getCode()) || Objects.equals(businessType, XbbRefTypeEnum.CREDIT_FIXED_LIMIT.getCode());
        if (!noEditButtonBusinessType.contains(businessType) && !isExpenseManagement) {

            if (hasFormEditPermission && hasDataEditpermission && !creditFlag){
                TopPermissionsPoJo editPoJo = new TopPermissionsPoJo(OperateTypeEnum.EDIT.getAlias(), OperateTypeEnum.EDIT.getName(), businessType, OperateTypeEnum.EDIT.getAlias());
                editPoJo.setBusinessType(XbbRefTypeEnum.transferBusinessType2Index(editPoJo.getBusinessType()));
                editPoJo.setDistributorMark(distributorMark);
                topPermissions.add(editPoJo);
            }
        }
        //没有删除功能的业务
        boolean noDel = Objects.equals(businessType, XbbRefTypeEnum.OTHER_INCOME.getCode()) || Objects.equals(businessType, XbbRefTypeEnum.OTHER_EXPENSE.getCode());

        //经销商类型，销项发票、回款单无删除按钮
        boolean specialHasDel = !Objects.equals(DistributorMarkEnum.DISTRIBUTOR.getCode(), distributorMark) || (!sheetFlag && !Objects.equals(businessType, XbbRefTypeEnum.INVOICE.getCode()));
        // 协同人不应该有删除合同的权限
        if (hasFormDeletePermission && !isExpenseManagement && specialHasDel && !Objects.equals(businessType, XbbRefTypeEnum.DISTRIBUTOR_ACCOUNT.getCode()) && !creditFlag && !noDel) {
            TopPermissionsPoJo deletePoJo = new TopPermissionsPoJo(OperateTypeEnum.DELETE.getAlias(), OperateTypeEnum.DELETE.getName(), businessType, OperateTypeEnum.DELETE.getAlias());
            deletePoJo.setDistributorMark(distributorMark);
            deletePoJo.setBusinessType(XbbRefTypeEnum.transferBusinessType2Index(deletePoJo.getBusinessType()));
            topPermissions.add(deletePoJo);
        }
        // 打印按钮
        PrintProPermissionAliasEnum printProPermissionAliasEnum = PrintProPermissionAliasEnum.getByCode(businessType);
        boolean hasPrint = Objects.nonNull(printProPermissionAliasEnum) && loginUserPermSet.contains(printProPermissionAliasEnum.getAlias());
        if (hasPrint ) {
            TopPermissionsPoJo printPoJo = new TopPermissionsPoJo("print", I18nMessageUtil.getMessage(PaasConstant.PRINT), businessType, "print");
            printPoJo.setDistributorMark(distributorMark);
            printPoJo.setBusinessType(XbbRefTypeEnum.transferBusinessType2Index(printPoJo.getBusinessType()));
            topPermissions.add(printPoJo);
        }
        return topPermissions;
    }
}
